<?php session_start();
	
class Disciplina{
	
	var $id;
	var $escola_id;
	var $nome;

	/* Getters and Setters */
	public function setId($value){
		$this->id = $value;
	}
	public function getId(){
		return $this->id;
	}
	
   	public function setNome($value){
		$this->nome = $value;
	}
	public function getNome(){
		return $this->nome;
	}
	
   	public function setEscola_id($value){
		$this->escola_id = $value;
	}
	public function getEscola_id(){
		return $this->escola_id;
	}
	/* Getters and Setters */

	
	function exibeDadosCombo($disciplina_id) {
	
		$con = new Conexao();
		
		
	
		$query = "SELECT id_disciplina,
						 nome,
						 escola_id
				  FROM disciplinas
				  WHERE escola_id = '".$_SESSION['escola_id']."'";		
		
		$result = $con->query($query);
		
		$retorno = "<option value=''>Escolha uma Disciplina</option>";
		
		while($row = $con->fetchArray($result) ){
			
			$selected = $disciplina_id==$row['id_disciplina'] ? "selected='selected'" : "" ; 
			
			$retorno .= "<option value='".$row['id_disciplina']."' $selected>".$row['nome']."</option>";
		}
		
		return $retorno;
	}

	function exibeDadosCombo2() {
	
		$con = new Conexao();
	
		$query = "SELECT id_disciplina,
						 nome
				  FROM disciplinas				  
				  WHERE escola_id = '".$_SESSION['escola_id']."'";	
		
		$result = $con->query($query);
		
		$retorno = "<option value=''>Escolha uma Disciplina</option>";
		
		while($row = $con->fetchArray($result) ){
			$retorno .= "<option value='".$row['id_disciplina']."'>".$row['nome']."</option>";
		}
		
		return $retorno;
	}
	
	
	function gridSalas($page,$limit,$sidx,$sord){
		
		$con = new Conexao();		
		
		$page  = $page; 
		$limit = $limit; 
		$sidx  = $sidx; 
		$sord  = $sord; 
			
		$queryCount = "SELECT COUNT(id_disciplina) as count
					   from disciplinas
					   WHERE escola_id = '".$_SESSION['escola_id']."'";
					 
		$resultSetCount = $con->query($queryCount);			 
					 
		$rowCount = $con->fetchArray($resultSetCount);
		$count = $rowCount['count'];
		
		if( $count>0 ){
			$total_pages = ceil($count/$limit);	
		}else{
			$total_pages = 0;
		}
		
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit;
		
		$query = "SELECT id_disciplina,
						 nome
				  FROM disciplinas
				  WHERE escola_id = '".$_SESSION['escola_id']."'
				  ORDER BY $sidx $sord 
				  LIMIT $start , $limit";				 
				 // echo $query;die;
						
		$resultSet = $con->query($query);
		
		$response->page = $page;
		$response->total = $total_pages;
		$response->records = $count;
		$i=0;
		
		while ( $rowDisciplina = $con->fetchArray($resultSet) ){
							
				$response->rows[$i]['id']=$rowDisciplina['id_disciplina'];
				$response->rows[$i]['nome']=$rowDisciplina['nome'];
				$i++;
					
		}	
		
		return $response;
	}


	function salvar($object){
		
		$con = new Conexao();
		 	 		 
		$query2 = "INSERT INTO disciplinas (
					   nome,
					   escola_id)
				   VALUES(
					   '".$object->nome."',
					   '".$object->escola_id."')";
				 
	    return $con->query($query2);
	
	}
	
	function atualizar($object){
		
		$con = new Conexao();
		
		$query2 = "UPDATE disciplinas SET
						  nome = '".$object->nome."'
				   WHERE id_disciplina = ".$object->id."";
				 
	    return $con->query($query2);
		
	}
	
	function deletar($object){
		
		$con = new Conexao();
		
		$query2 = "DELETE FROM disciplinas WHERE id_disciplina = ".$object->id."";
				 
	    return $con->query($query2);
		
	}
}

?>